namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AreaManage : Page
    {
        protected DataList AreaDL;
        protected Image iRegionManage;
        protected UserPrincipal Principal = null;

        private void AreaDL_CancelCommand(object source, DataListCommandEventArgs e)
        {
            this.AreaDL.EditItemIndex = -1;
            this.AreaDLDataBind();
        }

        private void AreaDL_DeleteCommand(object source, DataListCommandEventArgs e)
        {
        }

        private void AreaDL_EditCommand(object source, DataListCommandEventArgs e)
        {
            this.AreaDL.EditItemIndex = e.Item.ItemIndex;
            this.AreaDLDataBind();
        }

        private void AreaDL_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string str;
            string cmd = e.CommandName;
            if (((str = cmd) != null) && (string.IsInterned(str) == "new"))
            {
                TextBox NameTbx = (TextBox) e.Item.FindControl("txtNewName");
                TextBox SCNameTbx = (TextBox) e.Item.FindControl("txtNewSCName");
                if ((NameTbx != null) && (SCNameTbx != null))
                {
                    string AreaName = NameTbx.Text.Trim();
                    string AreaSCName = SCNameTbx.Text.Trim();
                    if ((AreaName != "") && (AreaSCName != ""))
                    {
                        if (ManageArea.IsExistSameAreaName(AreaName))
                        {
                            base.Response.Write("<script> alert('已存在该区') </script>");
                            this.AreaDLDataBind();
                        }
                        else
                        {
                            ManageArea.InsertNewArea(AreaName, AreaSCName);
                            this.AreaDLDataBind();
                        }
                    }
                }
            }
        }

        private void AreaDL_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            TextBox txtName = e.Item.FindControl("txtName") as TextBox;
            TextBox txtSCName = e.Item.FindControl("txtSCName") as TextBox;
            if ((txtName != null) && (txtSCName != null))
            {
                string AreaName = txtName.Text.Trim();
                string AreaSCName = txtSCName.Text.Trim();
                if (!(AreaName == "") && !(AreaSCName == ""))
                {
                    ManageArea.UpdateArea(AreaName, AreaSCName, (Guid) this.AreaDL.DataKeys[e.Item.ItemIndex]);
                    this.AreaDL.EditItemIndex = -1;
                    this.AreaDLDataBind();
                }
            }
        }

        private void AreaDLDataBind()
        {
            this.AreaDL.DataSource = ManageArea.GetArea();
            this.AreaDL.DataBind();
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("ProvinceManage", str, language, path);
        }

        private void InitializeComponent()
        {
            this.AreaDL.ItemCommand += new DataListCommandEventHandler(this.AreaDL_ItemCommand);
            this.AreaDL.CancelCommand += new DataListCommandEventHandler(this.AreaDL_CancelCommand);
            this.AreaDL.EditCommand += new DataListCommandEventHandler(this.AreaDL_EditCommand);
            this.AreaDL.UpdateCommand += new DataListCommandEventHandler(this.AreaDL_UpdateCommand);
            this.AreaDL.DeleteCommand += new DataListCommandEventHandler(this.AreaDL_DeleteCommand);
            base.Load += new EventHandler(this.Page_Load);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.SetFromImage();
                this.AreaDLDataBind();
            }
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iRegionManage.ImageUrl = "images/title_RegionManage.jpg";
                    break;

                case "en":
                    this.iRegionManage.ImageUrl = "images/title_RegionManageEn.jpg";
                    break;
            }
        }
    }
}

